package com.sinheldrin.musicplayer.reader
{
	import com.sinheldrin.musicplayer.fields.Field;
	import com.sinheldrin.musicplayer.fields.FieldClass;
	import com.sinheldrin.musicplayer.fields.FieldsController;
	import com.sinheldrin.musicplayer.library.LibraryController;
	import com.sinheldrin.musicplayer.music.Music;
	import com.sinheldrin.musicplayer.music.Playlist;
	import com.sinheldrin.musicplayer.settings.SettingsModel;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class XmlReader extends EventDispatcher implements IReader
	{
		public function XmlReader()
		{
		}
		
		public function readLibrary(library:LibraryController, file:File, fields:FieldsController):void
		{
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.READ);
			var xml:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
			for each (var musicXml:XML in xml.musics.music)
			{
				library.addMusic(readMusic(musicXml, fields));
			}
			fileStream.close();
		}
		
		public function read():Playlist
		{
			var file:File = File.desktopDirectory.resolvePath("test.xml");
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.READ);
			var xml:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
			var playlist:Playlist = parseXml(xml);
			fileStream.close();
			return playlist;
		}
		
		private function parseXml(xml:XML):Playlist
		{
			var playlist:Playlist = new Playlist();
			
			for each (var musicXml:XML in xml.musics.music)
			{
				playlist.addMusic(readMusic(musicXml, null));
			}
			
			return playlist;
		}
		
		private function readMusic(xml:XML, fields:FieldsController):Music
		{
			var music:Music = new Music();
			music.id = xml.@id;
			music.videoId = xml.@video_id;
			for each (var fieldXml:XML in xml.field)
			{
				music.fields.addItem(readField(fieldXml, fields));
			}
			return music;
		}
		
		private function readField(xml:XML, fields:FieldsController):Field
		{
			var field:Field = new Field(fields.getFieldClassById(xml.@classId));
			field.value = xml.@value;
			return field;
		}
		
		public function readSettings(settings:SettingsModel, file:File):Boolean
		{
			if (file.exists == false)
				return false;
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.READ);
			var xml:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
			
			settings.applicationXPosition = xml.applicationXPosition;
			settings.applicationYPosition = xml.applicationYPosition;
			
			fileStream.close();
			return true;
		}
		
		public function readFieldClasses(fields:FieldsController, file:File):Boolean
		{
			if (file.exists == false)
				return false;
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.READ);
			var xml:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
			
			for each (var fieldClassXml:XML in xml.classes.fieldClass)
			{
				fields.addFieldClass(readFieldClass(fieldClassXml));
			}
			
			fileStream.close();
			return true;
		}
		
		private function readFieldClass(xml:XML):FieldClass
		{
			var fieldClass:FieldClass = new FieldClass();
			fieldClass.id = xml.@id;
			fieldClass.name = xml.@name;
			fieldClass.isDefault = xml.@isDefault;
			return fieldClass;
		}
	}
}